// window.onload = function(){
const btn = document.querySelector('button');
const usr = document.getElementById('usr');
const pwd = document.getElementById('pwd');
// console.log(usr.value);
function Login_d(ador_name, ador_poss) {
    $.ajax({
        url: `http://127.0.0.1:7001/api/token`,
        type: "post",
        data: {
            ador_name,
            ador_poss
        },
        headers: {
            token: localStorage.getItem('mytoken')
        },

        success: function (res) {
            console.log(res);
            const vcode = document.getElementById("vcode");
            const yzm = vcode.value;
            const firtips = document.querySelector('.firtips');

            if (res.code === 0) {
                console.log(1111);
                if (yzm.toLocaleLowerCase() === code.toLocaleLowerCase()) {
                    alert('登录成功!')
                    console.log(res);
                    const usrs = usr.value;
                    console.log(usrs);
                    localStorage.setItem('mytoken', res.token);
                    localStorage.setItem('judgeqx', res.judge);
                    localStorage.setItem('ador_name', usrs);
                    window.location.href = './Index.html';

                } else if (yzm.length === 0) {
                    firtips.innerHTML = "请输入验证码!";
                } else if (yzm.toLocaleLowerCase() !== code.toLocaleLowerCase()) {
                    firtips.innerHTML = "输入正确的验证码！";
                    changeImg();
                }
            }else if (res.code === 4004) {
                firtips.innerHTML = "账号或密码错误,请重新输入!";
            } else{
                window.location.reload();
                alert('账号不存在');
            }
        },

    })
}
Login_d()

// const vcode = document.getElementById("vcode");
// console.log(vcode);
var code; //声明一个变量用于存储生成的验证码
const codes = document.getElementById("code")
changeImg();
codes.onclick = changeImg;

function changeImg() {
    var arr = new Array(
        '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
        'u', 'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
        'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
        'U', 'V', 'W', 'X', 'Y', 'Z');
    code = '';
    for (i = 0; i < 4; i++) {
        //随机生成四位数验证码
        var r = parseInt(Math.random() * arr.length)
        code += arr[r]
    }
    document.getElementById("code").innerHTML = code;
    console.log(code);
}
btn.onclick = function () {
    const firtips = document.querySelector('.firtips');
    if (usr.value === '') {
        firtips.innerHTML = "请输入用户名!";
    } else if (pwd.value === '') {
        firtips.innerHTML = "请输入密码!";
    } 
    console.log(usr.value, pwd.value);
    Login_d(
        usr.value,
        pwd.value
    )
}
const token = localStorage.getItem('mytoken')
// }